﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Data;

namespace LanguageInfo.Common.Utilities
{
    public class EnumsEntity
    {
        public int Value { get; set; }
        public string Text { get; set; }
    }
    /// <summary>
    /// 描述：枚举整合类
    /// 作者：方博
    /// 时间：2011-5-13
    /// </summary>
    public class EnumsHepler
    {
        /// <summary>
        /// 获取指定枚举中，对应枚举的 Description 的 Attribute 的值，
        /// 与枚举的索引组成匿名对象加入到 List 中返回，new { Text = text, Value = value }
        /// </summary>
        /// <param name="enumType">指定枚举的 Type</param>
        /// <returns>List</returns>
        public static List<object> GetEnumsToList(Type enumType)
        {
            int value;
            string text;
            object[] attr;
            List<object> resultList = new List<object>();
            Type descriptionType = typeof(DescriptionAttribute);

            foreach (FieldInfo fi in enumType.GetFields())
            {
                if (fi.FieldType.IsEnum)
                {
                    attr = fi.GetCustomAttributes(descriptionType, true);
                    text = attr.Length > 0 ? ((DescriptionAttribute)attr[0]).Description : fi.Name;
                    value = (int)enumType.InvokeMember(fi.Name, BindingFlags.GetField, null, null, null);
                    resultList.Add(new { Text = text, Value = value });
                }
            }
            return resultList;
        }

        /// <summary>
        /// 获取指定枚举中，对应枚举的 Description 的 Attribute 的值，
        /// 与枚举的索引组成匿名对象加入到 List 中返回，new { Text = text, Value = value }
        /// </summary>
        /// <param name="enumType">指定枚举的 Type</param>
        /// <returns>List</returns>
        public static List<EnumsEntity> GetEnumsToEntityList(Type enumType)
        {
            object[] attr;
            List<EnumsEntity> resultList = new List<EnumsEntity>();
            Type descriptionType = typeof(DescriptionAttribute);

            foreach (FieldInfo fi in enumType.GetFields())
            {
                if (fi.FieldType.IsEnum)
                {
                    EnumsEntity ojbE = new EnumsEntity();
                    attr = fi.GetCustomAttributes(descriptionType, true);
                    ojbE.Text = attr.Length > 0 ? ((DescriptionAttribute)attr[0]).Description : fi.Name;
                    ojbE.Value = (int)enumType.InvokeMember(fi.Name, BindingFlags.GetField, null, null, null);
                    resultList.Add(ojbE);
                }
            }
            return resultList;
        }
        /// <summary>
        /// 根据 value 读取指定枚举中的 Description 说明值
        /// </summary>
        /// <param name="enumType">指定枚举的 Type</param>
        /// <param name="value">值</param>
        /// <returns>string</returns>
        public static string GetEnumsDescription(Type enumType, int value)
        {
            int val;
            string text;
            object[] attr;
            Type descriptionType = typeof(DescriptionAttribute);

            foreach (FieldInfo fi in enumType.GetFields())
            {
                if (fi.FieldType.IsEnum)
                {
                    attr = fi.GetCustomAttributes(descriptionType, true);
                    text = attr.Length > 0 ? ((DescriptionAttribute)attr[0]).Description : fi.Name;
                    val = (int)enumType.InvokeMember(fi.Name, BindingFlags.GetField, null, null, null);
                    if (value == val)
                    {
                        return text;
                    }
                }
            }
            return string.Empty;
        }
    }
}
